<?php
/***************
* @Project	Warabi
* @Author	Feifei
* @Date		04/10/2009
***************/


/***These are the global functions for every module**/

/**to make the words security**/
/**todo : filter "select" "union"..keywords***/
function filterValue($string){
	if(is_array($string)){
		$temp = array_map('filterValue' , $string);
	}
	else{
		if(!get_magic_quotes_gpc()){
			$temp = addslashes($string);
		}
		else{
			$temp = $string;
		}
	}
	return $temp;
}


/**get the tid for response**/
function getTid(){
	$now = date(DATE_ATOM);
	//$arr = explode('+' , $now);
	$tid = substr($now , 0 , strlen($now)-6);
	return $tid;
}


/** base64 and md5 **/
function getEncryptKey($atom){
	$key = base64_encode(md5($atom,true));
	#$yy = base64_encode(pack("H*", md5("WIaBRa811098765432")));
	return $key;
}

/**wsse authorization**/
function magicWsse($xwsse , $db , $type){
	$xarr = explode("," , $xwsse);
	$devil = "UsernameToken";
	$wssebox = array();
	foreach($xarr as $x){
		if(!(strpos($x , $devil)===false)){
			$x = str_replace($devil , "" , $x);
		}
		$kv = explode('=' , $x ,2);
		$key = trim($kv[0]);
		$val = trim($kv[1]);
		$wssebox[$key] = substr($val , 1 , strlen($val)-2);
	}

	/** select the passcode **/
	if($type==0){
		$sql = "select password from ".__table_app_user__." where username='".$wssebox['Username']."'";
	}
	else if($type==1){
		$sql = "select password from ".__table_dpf_user__." where tel = '".$wssebox['Username']."'";
	}

	$set = $db->query($sql);
	$num = $db->countResult($set);

    while($passcode = $db->fetchResultBySet($set)){
			$atom = $wssebox["Nonce"].$wssebox["Created"].$passcode[0];
			$enKey = getEncryptKey($atom);
			if($enKey==$wssebox["PasswordDigest"]){
				$db->freeResult($set);
				return true;
			}
	}
	
	$db->freeResult($set);
	return false;
}


/** get the invitation flag by readflag | deleteflag | expireflag **/
/*value	status	
0	undel+unexpired+unread   (000)	
1	undel+unexpired+read     (001)	
2	undel+expired+unread     (010)	
3	undel+expired+read       (011)	
4	del+unexpired+unread     (100)	
5	del+unexpired+read       (101)	
6	del+expired+unread       (110)	
7	del+expired+read         (111)	
**/
function getInviteFlag($del,$expire,$read){
	return bindec($del.$expire.$read);
}

/** get the width fit for picasa width **/
function getPicasaWidth($width){
	global $wbPicasaWidth;
	$count = count($wbPicasaWidth);
	for($i=0,$l=$count;$i<$l;$i++){
		$w = $wbPicasaWidth[$i];
		$wn =  $wbPicasaWidth[$i+1];
		if($w == $width) return $width;
		if(($w<$width)&&($width<$wn)) return (($width-$w)<=($wn-$width))?$w:$wn;
		if(($i==0&&$width<$w)||($i==$count-1&&$width>$w)) return $w;
		continue;
	}
}

/** get the width ,height by the original width ,height**/
function getPicasaHeight($width , $oriwidth,$oriheight){
//	$width = getPicasaWidth(intval($width));
	$height = intval(($oriheight*$width)/$oriwidth);
	return $height;
}

/** get the proper size for the dpf **100131***/
function getPicasaSize($width , $height , $owidth , $oheight){
	if(($width/$height)>=($owidth/$oheight)){
		$pwidth = getPicasaWidth($width);
		$pheight = getPicasaHeight($pwidth , $owidth , $oheight);
		return array("width"=>$pwidth , "height"=>$pheight , "uri"=>$pwidth);
	}else{
		$pheight = getPicasaWidth($pheight);
		$pwidth = getPicasaHeight($pwidth , $oheight , $owidth);
		return array("width"=>$pwidth , "height"=>$pheight , "uri"=>$pheight);
	}
}

/** get the final url by the width to generate the sub dir **/
function getFinalUrl($url , $sub){
	$pos = strrpos($url , '/');
	$s1 = substr($url , 0 , $pos);
	$s2 = substr($url , $pos);
	$final = $s1.'/'.$sub.$s2;
	return $final;
}

/** xml body special chars**/
function xmlencode($str){
	$str_search = array("<" , ">" , "&" ,"\"" , "'");
	$str_replace = array("&lt;" , "&gt;" , "&amp;" , "&quot;" , "&apos;");
	$newstr = str_replace($str_search , $str_replace , $str);
	return $newstr;
}

?>